C++14 FAQs by Chandra Shekhar Kumar

C++14 FAQs by Chandra Shekhar Kumar

Author:Chandra Shekhar Kumar [Kumar, Chandra Shekhar]
Language: eng
Format: epub
Published: 2014-06-20T21:00:00+00:00


0.48

Custom Sequence : Addition

*** Question 48 Custom Sequence : Addition

Generate a custom sequence by addition of a constant to all the elements of a given integer sequence, i.e. std::integer_sequence<T, I...>

gets transformed to

std::integer_sequence<T, N+I...>

for a compile time constant N.

SOLUTION OF QUESTION 48

1#include <utility>

2

3template<int N, typename T, T... I>

4constexpr std::integer_sequence<T, N+I...>

5add(std::integer_sequence<T, I...>) noexcept

6{

7 return {};

8}

9

10using orig_seq =

11 std::integer_sequence<std::size_t, 1, 2, 3, 4, 5>;

12

13using expected_seq =

14 std::integer_sequence<std::size_t, 6, 7, 8, 9, 10>;

15

16auto add5_seq = add<5>(orig_seq());

17

18static_assert(std::is_same<decltype(add5_seq), expected_seq>::value, "");



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.